package com.qf.driver.api;

import com.qf.common.bean.R;
import com.qf.common.dto.DriverMqttDto;
import com.qf.driver.service.AuthService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author 千锋健哥
 */
@RestController
@RequestMapping("/driver/auth")
@Slf4j
public class AuthController {

    @Autowired
    private AuthService authService;

    @PostMapping("/login")
    public R<DriverMqttDto> login(@RequestBody DriverMqttDto dto) {
        log.info("[driver-mqtt设备中心]=======认证开始=========");
        //1. 判断dto不能为空
        if (dto == null) {
            return R.fail("请传入认证的参数!");
        }
        //2. 判断产品key, 设备key, 设备秘钥不为空
        if(StringUtils.isEmpty(dto.getProductKey())
                || StringUtils.isEmpty(dto.getDeviceKey())
                || StringUtils.isEmpty(dto.getDeviceSecret())) {
            return R.fail("设备key, 产品key, 设备秘钥不能为空!");
        }

        //3. 调用service进行认证
        R<DriverMqttDto> result = authService.login(dto);
        log.info("[driver-mqtt设备中心]=======认证结束=========");
        //4. 返回
        return result;
    }

}
